package com.zide.file.config;

import com.zide.common.utils.ServletUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author 石添
 * @date 2024/4/29 16:40
 */
@Slf4j
@Component
public class LocalFileConfig {

    private static String uploadPath = "";

    private static String IMG_PATH = "/img/";
    private static String AVATAR_PATH = "/img/avatar/";

    // 图片上传路径
    public static String getUploadImgPath() {

        return uploadPath + IMG_PATH;
    }

    public static String getUploadAvatarPath() {

        return uploadPath + AVATAR_PATH;
    }

    public static String getAvatarPath() {
        HttpServletRequest request = ServletUtils.getRequest();
        StringBuffer url = request.getRequestURL();
        String contextPath = request.getServletContext().getContextPath();
        return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).append(AVATAR_PATH).toString();
    }

    @Value("${zide.upload_path}")
    public void setUploadImgPath(String uploadPath) {
        LocalFileConfig.uploadPath = uploadPath;
    }

    public static String getImgPath() {
        HttpServletRequest request = ServletUtils.getRequest();
        StringBuffer url = request.getRequestURL();
        String contextPath = request.getServletContext().getContextPath();
        return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).append(IMG_PATH).toString();
    }
}
